package com.client.elements.canvas;

import com.client.elements.canvas.tools.Kreis;
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class Kreis_Striche {
	
	static Canvas canvas;
	static Context2d context;
	
	final static Button btn = new Button("Start");
	
	static Kreis gross = new Kreis(150, 200, 200, 0, 0);
	static Kreis mitte = new Kreis(15, 200, 200, 0, 0);

	public static Widget draw() {

		FlowPanel main = new FlowPanel();

		canvas = Canvas.createIfSupported();

		canvas.setCoordinateSpaceWidth(400);
		canvas.setCoordinateSpaceHeight(400);
		canvas.addStyleName("border");

		context = canvas.getContext2d();

		timer(true);

		btn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (btn.getText().equals("Start")) {
					context.clearRect(0, 0, 400, 400);
					gross.update(context);
					mitte.update_bunt(context, "black");
					winkel = 180;
					timer(false);
					btn.setText("Stop");
					gross.update(context);
					mitte.update_bunt(context, "black");
				}else if (btn.getText().equals("Stop")) {
					timer(true);
					btn.setText("Start");
				}
			}
		});

		main.add(canvas);
		main.add(btn);

		return main;
	}
	
	final static Timer timer = new Timer() {
		@Override
		public void run() {
			drawWatch();
		}
	};

	private static void timer(boolean stop) {
		if (stop == false) {
			timer.scheduleRepeating(10);		
		}else {
			timer.cancel();
		}
	}
	
	static double winkel;
	
	public static void drawWatch(){
		context.beginPath();
		context.moveTo(200, 200);
		double pos_y = Math.cos(winkel) * 150 + 200;
		double pos_x = Math.sin(winkel) * 150 + 200;
		
		context.lineTo(pos_x, pos_y);
		context.closePath();
		context.stroke();
		
		winkel += 6;
	}
}
